<section id = "doc_event-drive" data-view = "true" data-view-title = "事件驱动">
	<header>
		<a class = "nav-back" data-view-rel = ":back"></a>
		<span>事件驱动</span>
		<a class = "menu"></a>
	</header>
	<div class = "body">
		<p>所有视图实例均具备事件驱动特性。</p>
		<p>视图支持的事件包括：</p>
		<ol>
			<li>
				View事件：<span class = "code js">beforechange</span> - 视图将要切换
				<p class = "no-text-indent">监听样例：</p>
				<div class = "code js">View.on("beforechange", function(e){});</div>
			</li>
			<li>
				View实例事件：<span class = "code js">ready</span> - 视图就绪
				<p class = "no-text-indent">监听样例：</p>
				<div class = "code js">View.ofId("myView").on("ready", function(e){});</div>
				<p class = "no-text-indent">注：特定视图的ready事件只会在视图第一次进入时触发一次。视图第二次进入后不会再触发</p>
			</li>
			<li>
				View实例事件：<span class = "code js">beforeenter</span> - 视图将要进入
				<p class = "no-text-indent">监听样例：</p>
				<div class = "code js">View.ofId("myView").on("beforeenter", function(e){});</div>
			</li>
			<li>
				View实例事件：<span class = "code js">enter</span> - 视图进入
				<p class = "no-text-indent">监听样例：</p>
				<div class = "code js">View.ofId("myView").on("enter", function(e){});</div>
			</li>
			<li>
				View实例事件：<span class = "code js">afterenter</span> - 视图进入完成
				<p class = "no-text-indent">监听样例：</p>
				<div class = "code js">View.ofId("myView").on("afterenter", function(e){});</div>
			</li>
			<li>
				View事件：<span class = "code js">afterchange</span> - 视图切换完成
				<p class = "no-text-indent">监听样例：</p>
				<div class = "code js">View.on("afterchange", function(e){});</div>
			</li>
		</ol>

		<p>除此之外，开发者还可以根据自己需要，发起自定义事件并为这些事件添加监听器。如：</p>
		<div class = "code js">
var view = View.ofId("myView");
view.on("myevent", function(e){
	view.logger.debug("Event name: {}, event data: {}", e.name, e.data);
});
//…
view.fire("myevent", {a: 1});//-> 0713 10:20:54 [View#myView]: Event name: null, event data: {"a":1}
		</div>
		<p>开发者在创建自定义事件时，需注意事件名的可读性以及见名知意的直观性。虽然任何形式的命名都能驱动程序的正常工作，但处于工程的可维护性，并不建议这样做。</p>
		<p>开发者在发起自定义事件时，可以为事件附加任意类型的数据。对应的事件监听器在捕获对应的事件时，可以通过"data"属性获取附加的数据。如：</p>
		<div class = "code js" data-filename = "a.js">
View.ofId("detail").fire("goodsDetail.obtained", {goodsName: "XXXX", price: 12});
		</div>

		<div class = "code js" data-filename = "b.js">
View.ofId("detail").on("goodsDetail.obtained", function(e){
	console.log(e.data);// -> {goodsName: "XXXX", price: 12}
});
		</div>

		<p>除此之外，开发者还可以通过API：<span class = "code js">view.getLatestEventData(evtName)</span>获取指定名称的事件最后一次被触发时所附加的数据，如：</p>
		<div class = "code js" data-filename = "c.js">
var goodsDetail = View.ofId("detail").getLatestEventData("goodsDetail.obtained");
console.log(goodsDetail);// -> {goodsName: "XXXX", price: 12}
		</div>
	</div>
	<footer><div class = "btn next">下一节</div></footer>
</section>